<!DOCTYPE html>












  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=6.5.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.5.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.5.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.5.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.5.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.5.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="最近公司的项目有一个上传文件的需求，由于项目是基于Spring Boot构建的，因为只是一个小型项目，也没有用到云存储，或者FastDFS之类的分布式文件系统，而是直接写在当前服务器的磁盘上。在打成最终的jar包之后，出现了无法往jar包里设定的的classpath路径下的上传路径中写入文件。经过一番研究后找到解决方案，现记录如下。">
<meta name="keywords" content="Spring Boot">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Boot项目使用服务器磁盘路径上传文件">
<meta property="og:url" content="https://lisb.gitee.io/2018/03/25/2018-03-25-Spring-Boot项目使用服务器磁盘路径上传文件/index.html">
<meta property="og:site_name" content="码畜·小布">
<meta property="og:description" content="最近公司的项目有一个上传文件的需求，由于项目是基于Spring Boot构建的，因为只是一个小型项目，也没有用到云存储，或者FastDFS之类的分布式文件系统，而是直接写在当前服务器的磁盘上。在打成最终的jar包之后，出现了无法往jar包里设定的的classpath路径下的上传路径中写入文件。经过一番研究后找到解决方案，现记录如下。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-05-04T06:58:17.638Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Boot项目使用服务器磁盘路径上传文件">
<meta name="twitter:description" content="最近公司的项目有一个上传文件的需求，由于项目是基于Spring Boot构建的，因为只是一个小型项目，也没有用到云存储，或者FastDFS之类的分布式文件系统，而是直接写在当前服务器的磁盘上。在打成最终的jar包之后，出现了无法往jar包里设定的的classpath路径下的上传路径中写入文件。经过一番研究后找到解决方案，现记录如下。">






  <link rel="canonical" href="https://lisb.gitee.io/2018/03/25/2018-03-25-Spring-Boot项目使用服务器磁盘路径上传文件/">



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Spring Boot项目使用服务器磁盘路径上传文件 | 码畜·小布</title>
  











  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">码畜·小布</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lisb.gitee.io/2018/03/25/2018-03-25-Spring-Boot项目使用服务器磁盘路径上传文件/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="小布">
      <meta itemprop="description" content="曾梦想仗剑走天涯，后来bug太多就没去">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="码畜·小布">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Spring Boot项目使用服务器磁盘路径上传文件
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-03-25 13:24:36" itemprop="dateCreated datePublished" datetime="2018-03-25T13:24:36+08:00">2018-03-25</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-05-04 14:58:17" itemprop="dateModified" datetime="2018-05-04T14:58:17+08:00">2018-05-04</time>
              
            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>最近公司的项目有一个上传文件的需求，由于项目是基于Spring Boot构建的，因为只是一个小型项目，也没有用到云存储，或者FastDFS之类的分布式文件系统，而是直接写在当前服务器的磁盘上。在打成最终的jar包之后，出现了无法往jar包里设定的的classpath路径下的上传路径中写入文件。经过一番研究后找到解决方案，现记录如下。<br><a id="more"></a><br>首先明确一点，我们希望将上传的内容置于所有用户都能访问的路径，而Spring Boot中的静态资源路径正有着这样的概念。<br><strong>静态资源路径是指系统可以直接访问的路径，且路径下的所有文件均可被用户直接读取。</strong><br>因此实际上我们要明确的事情只有两件：<br>1.将外部的磁盘路径作为工程的一部分可以被工程直接访问；<br>2.对这个路径的访问不需要进行权限控制。<br>回归Spring Boot,在Spring Boot中默认的静态资源路径有：classpath:/META-INF/resources/，classpath:/resources/，classpath:/static/，classpath:/public/，从这里可以看出这里的静态资源路径都是在classpath中（也就是在项目路径下指定的这几个文件夹）。<br>在Spring Boot中可以直接在配置文件中对默认的静态资源路径的配置进行覆盖。<br>application.yml配置文件部分内容如下：</p>
<pre><code class="yaml"><span class="attr">spring:</span>
<span class="attr">  http:</span>
<span class="attr">    multipart:</span>
<span class="attr">      max-file-size:</span> <span class="number">10</span><span class="string">MB</span>
<span class="attr">      max-request-size:</span> <span class="number">10</span><span class="string">MB</span>
<span class="attr">      location:</span> <span class="string">上传文件夹的服务器磁盘绝对路径</span>
<span class="attr">    encoding:</span>
<span class="attr">      enabled:</span> <span class="literal">true</span>
<span class="attr">      force:</span> <span class="literal">true</span>
<span class="attr">      charset:</span> <span class="string">UTF-8</span>
  <span class="comment">#所有的访问都经过静态资源路径</span>
<span class="attr">  mvc:</span>
<span class="attr">    static-path-pattern:</span> <span class="string">/**</span>
<span class="attr">  resources:</span>
<span class="attr">    static-locations:</span> <span class="attr">classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${spring.http.multipart.location}</span>
</code></pre>
<p><strong>注意：</strong></p>
<ol>
<li>spring.http.multipart.location是Spring上传机制的默认上传路径配置名称，这个地方同样也可以使用自定义的属性。要注意的一点是，这个属性指定的路径注意要以/结尾；</li>
<li>spring.mvc.static-path-pattern=/**表示所有的访问都经过静态资源路径；</li>
<li>spring.resources.static-locations在这里配置静态资源路径，前面说了这里的配置是覆盖默认配置，所以一旦重写这个属性需要将原先Spring默认的那些静态资源路径也加上，否则static、public等这些路径将不能再被当作静态资源路径。</li>
<li>配置文件最末尾的file:${spring.http.multipart.location}之所有要加file:是因为这里指定的是一个具体的硬盘路径，其他的使用classpath指的是项目的根目录。</li>
</ol>

      
    </div>

    

    
    
    

    

    
       
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/04/01/2018-04-01-Spring-Security基本原理/" rel="prev" title="Spring Security基本原理">
                Spring Security基本原理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">小布</p>
              <p class="site-description motion-element" itemprop="description">曾梦想仗剑走天涯，后来bug太多就没去</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">标签</span>
                  
                </div>
              
            </nav>
          

          

          

          

          
          

          
            
          
          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小布</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v6.5.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.5.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.5.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.5.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.5.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.5.0"></script>



  



  










  





  

  

  

  

  

  
  

  

  

  

  

  

  

</body>
</html>
